package proj.unions.general;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.util.Log;

public class L {

	private static final String mSpecialFileName = "MSL.tmp";
	private static final String mFileName = "log_";
	private static final int fileCount = 50;
	private static boolean flag = false;
	
	public static void d(String arg0)
	{
		if ( SuperTools.isDebug )
		{
			if(!flag)
				init();
			Log.e("L", "---->" + arg0);
			writeLog(arg0);
		}
	}
	
	public static void d2(String arg0)
	{
		if ( SuperTools.isDebug )
		{
			Log.e("L", "-->>>" + arg0);
			writeSpecialLog(arg0);
		}
	}
	
	public static String getLogPath()
	{
		String logPath = SuperTools.getInstance().getAPKPath() + "Log/";
		return logPath;
	}
	
	public static void init()
	{
		String logPath = getLogPath();
		
		File file = new File(logPath);
		if ( !file.exists() ) {
			file.mkdir();
		}
		
		//最后一个
		file = new File(logPath + mFileName + fileCount);
		if ( file.exists() ) 
			file.delete();
		
		for(int i = fileCount - 1; i > 0 ; i--)
		{
			file = new File(logPath + mFileName + i);
			if ( file.exists() )
				file.renameTo( new File(logPath + mFileName + (i + 1)) );
		}
		flag = true;
	}
	
	public static void writeLog(String arg0)
	{
		String logPath = getLogPath();
		
		try {
			File file = new File(logPath + mFileName + "1");
			if ( !file.exists() )
				file.createNewFile();
			
			FileOutputStream outputStream = new FileOutputStream(file, true);
			arg0 += "\n";
			outputStream.write(arg0.getBytes());
			outputStream.flush();
			outputStream.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	
	public static void writeSpecialLog(String arg0)
	{
		String logPath = getLogPath();
		
		try {
			File file = new File(logPath + mSpecialFileName);
			if ( !file.exists() )
				file.createNewFile();
			
			FileOutputStream outputStream = new FileOutputStream(file, true);
			arg0 += "\n";
			
			String newString = getCurTime() + "->" + arg0;
			
			outputStream.write(newString.getBytes());
			outputStream.flush();
			outputStream.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}
	
//	public static String readLog()
//	{
//		String logPath = getLogPath();
//	
//		try {
//			File file = new File(logPath + mFileName);
//			if ( !file.exists() )
//				return "";
//			
//			FileInputStream inputStream = new FileInputStream(file);
//			byte[] bytes = new byte[1024];
//			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
//			while (inputStream.read(bytes) != -1) {
//				arrayOutputStream.write(bytes, 0, bytes.length);
//			}
//			inputStream.close();
//			arrayOutputStream.close();
//			
//			String content = new String(arrayOutputStream.toByteArray());
//			return content;
//			
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		return "";
//	}
	
	public static String getCurTime()
    {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Date date = new Date(System.currentTimeMillis());
    	return format.format(date);
    }
	
}
